import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/expert/expert_all_page_logic.dart';
import 'package:sports_new/entity/match/expert_views_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/api/expert_api.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/pick/items/avator_label.dart';
import 'package:sports_new/widgets/focus_button.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';

import '../../widgets/base/qkd_text.dart';

class ExpertAllPage extends StatelessWidget {
  ExpertAllPage({Key? key}) : super(key: key);

  final controller = Get.put(ExpertAllLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ExpertAllLogic>(builder: (controller) {
      return Scaffold(
        appBar: Styles.appBar(
          backgroundColor: Colours.transparent,
          title: SizedBox(
            height: 34,
            child: Styles.tabbar(
              tabs: const [QkdText("足球"), QkdText("篮球")],
              controller: controller.tabController,
              scrollable: true,
              labelPadding: const EdgeInsets.symmetric(horizontal: 12),
              fontSize: 18,
              onTap: (index) {},
            ),
          ),
        ),
        backgroundColor: Colours.scaffoldBg,
        body: Column(
          children: [
            Expanded(
              child: TabBarView(
                  controller: controller.tabController,
                  children: [const ExpertAllList(1), const ExpertAllList(2)]),
            ),
          ],
        ),
      );
    });
  }
}

class ExpertAllList extends StatefulWidget {
  //1足球 2篮球
  final int type;
  const ExpertAllList(this.type, {super.key});

  @override
  State<ExpertAllList> createState() => _ExpertAllListState();
}

class _ExpertAllListState extends State<ExpertAllList>
    with AutomaticKeepAliveClientMixin {
  final EasyRefreshController refreshC =
      EasyRefreshController(controlFinishLoad: true);
  List<PlanEntity>? list;
  int page = 1;
  int pageSize = 20;
  bool firstLoading = true;

  @override
  void initState() {
    super.initState();
    getNew();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return LoadingWidget(
      isLoading: firstLoading,
      data: list?.length ?? 0,
      child: () => list.hasValue
          ? EasyRefresh(
              controller: refreshC,
              onRefresh: () async {
                await getNew();
              },
              onLoad: () async {
                await getMore();
              },
              child: ListView.builder(
                  itemBuilder: (context, index) {
                    return cell(index);
                  },
                  itemCount: list!.length))
          : Container(),
    );
  }

  cell(int index) {
    PlanEntity data = list![index];
    return Styles.card(
      color: Colours.white,
      margin: const EdgeInsets.fromLTRB(8, 8, 8, 0),
      child: Column(
        children: [
          Container(
            padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  GestureDetector(
                      onTap: () {
                        Get.toNamed(Routes.userPage,
                            arguments: data.expertId,
                            parameters: {'type': '2'});
                      },
                      child: Container(
                          width: Get.width / 2,
                          height: 50,
                          color: Colours.transparent,
                          child: AvatarLabel.record(
                              avatarUrl: data.expertLogo,
                              name: data.expertName,
                              firstTag: data.firstTag,
                              secondTag: data.secondTag,
                              id: data.expertId))),
                  FocusButton(data.isFocus ?? 0, expertId: data.expertId,
                      onTap: (value) {
                    data.isFocus = value;
                  }),
                ]),
          ),
          Container(
            margin: const EdgeInsets.only(left: 16),
            height: 0.5,
            color: Colours.greyEE,
          )
        ],
      ),
    );
  }

  getNew() async {
    page = 1;
    final result =
        await ExpertApi.getExpertAll(page, pageSize, widget.type, '');
    firstLoading = false;
    if (result != null) {
      list = result;
      refreshC.resetFooter();
    }
    update();
  }

  getMore() async {
    page++;
    final result =
        await ExpertApi.getExpertAll(page, pageSize, widget.type, '');
    if (result != null) {
      list!.addAll(result);
      refreshC.finishLoad(result.length < 20
          ? IndicatorResult.noMore
          : IndicatorResult.success);
    }
    update();
  }

  @override
  bool get wantKeepAlive => true;
}
